<?php
namespace app\index\controller;

class Notice  {
	public function index() {
		$type = input('get.type');
		$user_id = input('session.user_id');
		$at_data_list = null;
		$reply_data_list = null;
		$like_data_list = null;
		$system_data_list = null;
		$show_num = $this->getNum($user_id);
		switch ($type) {
			case '1':
				$at_data_list = $this->getAtNoRead($user_id);
				break;
			case '2':
				$reply_data_list['text'] = $this->getReplyNoRead($user_id, '3');
                $reply_data_list['topic'] = $this->getReplyNoRead($user_id, '6');
				break;
			case '3':
				$like_data_list = $this->getLikeNoRead($user_id);
				break;
			case '4':
				$system_data_list = $this->getSystemNoRead($user_id);
				break;
			default:
				break;
		}

		$like_list = array();
        $clike = model('Like');
        $likes_data = $clike::where('uid', $user_id)->where('if_like', 1)->select();
        if ($likes_data) {
            foreach($likes_data as $like_data) {
                array_push($like_list, $like_data->tid);
            }
        }

        return view('/index/notice', [
        	'at_data_list' => $at_data_list,
        	'reply_data_list' => $reply_data_list,
        	'like_data_list' => $like_data_list,
        	'system_data_list' => $system_data_list,
        	'show_num' => $show_num,
        	'like_list' => $like_list
        ]);
    }

    public function getAllNum() {
    	$user_id = input('session.user_id');
    	if (!$user_id) {
    		return json([
                'status' => 'error',
                'message' => '未登录！',
                'data' =>  null
            ]);
    	}
    	$cmsglist = model('Msglist');
    	$at_num = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', 'in', '0,1,2')->count();
    	$reply_num = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', 'in', '3,6')->count();
    	$like_num = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', '4')->count();
    	$system_num = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', '5')->count();

    	$all_num = $at_num + $reply_num + $like_num + $system_num;
    	return json([
            'status' => 'success',
            'message' => '查询成功！',
            'data' =>  $all_num
        ]);
    }

    public function setRead() {
    	$type = input('get.type');
    	$sender = input('get.sender');
    	$user_id = input('session.user_id');

    	if (!$user_id) {
    		return json([
	            'status' => 'error',
	            'message' => '未登录！',
	            'data' =>  null
	        ]);
    	}

    	if ($type != null and $sender != null) {
	    	$cmsglist = model('Msglist');
			$cmsglist->save([
			    'if_read'  => 1
			],['sender' => $sender, 'receiver' => $user_id, 'type' => $type]);
    		return json([
	            'status' => 'success',
	            'message' => '成功！',
	            'data' =>  null
	        ]);
    	}
    	else {
			return json([
	            'status' => 'error',
	            'message' => '发送错误，请重试！',
	            'data' =>  null
	        ]);
    	}
    	
    }

    private function getNum($user_id) {
    	$num_list = array();
    	$cmsglist = model('Msglist');
    	$at_num = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', 'in', '0,1,2')->count();
    	$reply_num = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', 'in', '3,6')->count();
    	$like_num = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', '4')->count();
    	$system_num = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', '5')->count();

    	$num_list['at'] = $at_num;
    	$num_list['reply'] = $reply_num;
    	$num_list['like'] = $like_num;
    	$num_list['system'] = $system_num;
    	return $num_list;
    }

    private function getAtNoRead($user_id) {
    	$at_data_list = array();
    	$cmsglist = model('Msglist');
    	$data_list = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', 'in', '0,1,2')->order('type')->select();
    	if (!$data_list) {
    		return null;
    	}
    	$text_id_list = array();
    	$reply_id_list = array();
        $topic_reply_id_list = array();
    	foreach ($data_list as $data) {
            if ($data->type == 0) {
                array_push($topic_reply_id_list, $data->sender);
            }
    		if ($data->type == 1) {
    			array_push($text_id_list, $data->sender);
    		}
    		if ($data->type == 2) {
    			array_push($reply_id_list, $data->sender);
    		}
    	}

    	$text_list = $this->getText($text_id_list);
    	$reply_list = $this->getReply($reply_id_list, '3');
        $topic_reply_list = $this->getReply($topic_reply_id_list, '6');

    	$at_data_list['text'] = $text_list;
    	$at_data_list['reply'] = $reply_list;
        $at_data_list['topic_reply'] = $topic_reply_list;

    	return $at_data_list;
    }

    private function getReplyNoRead($user_id, $type) {
    	$reply_data_list = array();
    	$cmsglist = model('Msglist');
    	$data_list = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', $type)->select();
		if (!$data_list) {
    		return null;
    	}
    	$reply_id_list = array();
    	foreach ($data_list as $data) {
			array_push($reply_id_list, $data->sender);
    	}
    	$reply_data_list = $this->getReply($reply_id_list, $type);
    	return $reply_data_list;
    }

    private function getLikeNoRead($user_id) {
    	$like_data_list = array();
    	$cmsglist = model('Msglist');
    	$data_list = $cmsglist::where('receiver', $user_id)->where('if_read', 0)->where('type', '4')->select();
    	if (!$data_list) {
    		return null;
    	}

    	$like_id_list = array();
    	$update_list = array();
    	foreach ($data_list as $data) {
			array_push($like_id_list, $data->sender);
			$update = array();
    		$update['id'] = $data->id;
    		$update['if_read'] = 1;
    		array_push($update_list, $update);
    	}
		$like_data_list = $this->getLike($like_id_list);
		$cmsglist->saveAll($update_list);
		return $like_data_list;
    }

    private function getSystemNoRead($user_id) {

    }

    private function getText($id_list) {
    	if (count($id_list) == 0) {
    		return null;
    	}
    	$ctext = model('Text');
    	$text_list = $ctext::all($id_list);
    	return $text_list;
    }

    private function getReply($id_list, $type) {
    	if (count($id_list) == 0) {
    		return null;
    	}
        if ($type == '3') {
            $creply = model('Reply');
            $reply_list = $creply::all($id_list);
        }
        else {
            $ctopic_reply = model('TopicReply');
            $reply_list = $ctopic_reply::all($id_list);
        }

		return $reply_list;
    }

    private function getLike($id_list) {
    	if (count($id_list) == 0) {
    		return null;
    	}
		$clike = model('Like');
		$clike_list = $clike::all($id_list);
		return $clike_list;
    }
}